const { Controller } = require('egg');

class ProductController extends Controller {


  /**
   * @api {GET} /product/product/list 获取产品列表
   * @apiName list
   * @apiGroup product
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } name 名称
   * @apiQuery  { String } type 类型
   * @apiQuery  { String } describe 描述
   * @apiQuery  { String } order 排序字段
   * @apiQuery  { String } method 排序方式
   * @apiQuery  { Number } pageIndex 页码
   * @apiQuery  { Number } pageSize 页面容量
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async list() {
    const ctx = this.ctx;
    const app = this.app;
    try {
      const { query } = ctx;
      const { name, type, describe, order, method, pageIndex, pageSize } = await app.rule.product.list(query);
      const data = await ctx.service.product.list(name, type, describe, order, method, pageIndex, pageSize);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /product/product/insert 新增
   * @apiName insert
   * @apiGroup product
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } name 名称
   * @apiBody  { String } type 类型
   * @apiBody  { String } describe 描述
   * @apiBody  { Number } file_id 文件id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async insert() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.request.body;

      const { name, describe, type, file_id } = await app.rule.product.insert(value);

      const data = await ctx.service.product.insert(name, describe, type, file_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {POST} /product/product/update 更新
   * @apiName update
   * @apiGroup product
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } name 名称
   * @apiBody  { String } type 类型
   * @apiBody  { String } describe 描述
   * @apiBody  { Number } file_id 文件id
   * @apiBody  { String } id 产品id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async update() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.request.body;

      const { name, describe, type, file_id, id } = await app.rule.product.update(value);

      const data = await ctx.service.product.update(name, describe, type, file_id, id);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /product/product/delete 删除
   * @apiName delete
   * @apiGroup product
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { Array } ids ids
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async delete() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.request.body;

      const { ids } = await app.rule.product.delete(value);

      const data = await ctx.service.product.delete(ids);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /product/product/info 删除
   * @apiName info
   * @apiGroup product
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } uuid 产品id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async info() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.query;

      const { uuid } = await app.rule.product.info(value);

      const data = await ctx.service.product.info(uuid);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = ProductController;
